package com.io.client;

import com.alibaba.fastjson.JSON;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

import java.util.Properties;
import java.util.Random;

/**
 * Created by 41579 on 2017/5/12.
 */
public class main {
    public static void main(String[] args) {
//        while(true) {
//            for(int i=0;i<40;i++){
//                JProducer pro = new JProducer("rentermogo");
//                pro.start();
//            }
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }

//        JProducer pro = new JProducer("rentermogo");
//        pro.start();
//        Boolean b = false;
//        StringBuffer sb = new StringBuffer();
//        sb.append(b);
//        System.out.println(sb.toString());

//        JConsumer con = new JConsumer(KafkaProperties.TOPIC);
//        con.start();

//        String a = "[1234567]req";
//        System.out.println(a.indexOf("0"));
//        String b = a.substring(a.indexOf("[")+1,a.indexOf("]"));
//        System.out.println(b);


        JProducer pro = new JProducer("tp_sync_community");
        pro.start();
    }

    public static class JProducer extends Thread {

        private final int SLEEP = 0 * 3;
        private Producer<Integer, String> producer;
        private String topic;
        private Properties props = new Properties();

        public JProducer(String topic) {
            props.put("serializer.class", "kafka.serializer.StringEncoder");
            props.put("metadata.broker.list", "192.168.40.58:9092");
            producer = new Producer<Integer, String>(new ProducerConfig(props));
            this.topic = topic;
        }

        @Override
        public void run() {
            Room room = new Room();
//            room.setRoomId(2227);
//            room.setFreeBeginDate("2017-05-01");
//            room.setFreeEndDate("2017-06-01");
//            room.setBedroomCount(2);
////            room.setCheckInTime("2017-01-19");
//            room.setFlatTag(2);
//            room.setCommunityCode("1001");
//            room.setNickName("测试李亚林2");
//            room.setParlorCount(2);
//            room.setToiletCount(2);
//            room.setTotalFloorCount(2);
//            room.setRoomArea(BigDecimal.valueOf(200));
//            room.setRentStatus(1);
//            room.setIntro("修改测试");
//            room.setFreeDeposit(1);
//            room.setLandlordId(3100080);
//            room.setRentTypeValue(2);
//            room.setOnlineStatus(1);
//            room.setRoomConfigs(new String[]{"1","2","3"});

            Integer[] landlordids = {1,2,3,4,5,6,7,8,9,3100080};
            Random random = new Random();
            Integer i = random.nextInt(10);

            room.setFlatTag(1);
            room.setRoomId(25522);
            room.setRentStatus(2);
            room.setOnlineStatus(2);
            Community community = new Community();
            community.setCommunityId(169694);
            community.setSynTPRoom(false);
            SyncRoomStatusMsg syncRoomStatusMsg = new SyncRoomStatusMsg();
            syncRoomStatusMsg.setLandlordId(landlordids[i]);
//            String msg = new String(JSON.toJSONString(community));
//            String msg = new String(JSON.toJSONString(room));
            String msg = new String(JSON.toJSONString(syncRoomStatusMsg));
//            String msg = new String("room-33061-123");
            msg = "2144337";
            System.out.println("Send->【" + msg + "】");
            producer.send(new KeyedMessage<Integer, String>(topic, msg));
            try {
                sleep(SLEEP);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
